module.exports = class Log {
  constructor() {
    this.MAX_LENGTH = 70
    this.indentSignal = '#'
    this.empty = ' '

    this.currentStep = 0
  }

  _createIndent(title) {
    const core = ` ${title} `
    const pad = num => this.indentSignal.repeat(num)
    const padString = pad(this.MAX_LENGTH - core.length)
    const mid = `${padString}${core}${padString}`
    const twoSide = pad(mid.length)

    return [
      twoSide,
      mid,
      twoSide
    ]
  }

  _createStep(value) {
    return [
      this.empty,
      `>>>>>> Step${++this.currentStep}. ${value}`,
    ]
  }

  _createSuccess() {
    return [
      this.empty,
      `✔️构建成功`.split('').join(' ') + ' 🎉🎉🎉',
      this.empty
    ]
  }

  _createFail(error) {
    const messages = [
      this.empty,
      `×构建失败`.split('').join(' ') + ' ❌',
      this.empty
    ]
    if (typeof error === 'object') {
      if (error.message) {
        const temp = messages[2]
        messages[2] = error.message.split('\n').map(t => `    ${t}`).join('\n')
        messages.push(temp)
      }
    }
    return messages
  }

  _output(array) {
    for (const item of Array.isArray(array) ? array : [array]) {
      console.log(item)
    }
  }

  title(title) {
    this._output(this._createIndent(title))
  }

  step(value) {
    this._output(this._createStep(value))
  }

  success() {
    this._output(this._createSuccess())
  }

  fail(error) {
    this._output(this._createFail(error))
  }

  info(value) {
    this._output(value)
  }
}